﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using RedeSocial.DataAccess;

namespace RedeSocial.Web
{
	public partial class AdminMaster : BaseMasterPage
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			if (!Request.IsAuthenticated)
				Response.Redirect("~/notauth.aspx");
			else
			{
				if (Session["session"] == null)
				{
					string email = FormsAuthentication.Decrypt(Request.Cookies[FormsAuthentication.FormsCookieName].Value).Name;
					User user;
					Session session;
					using (var dataContext = new RedeSocialEntities())
					{
						session = dataContext.Sessions.Include("User").Include("User.Country").Include("User.File").Where(s => s.User.Email.Equals(email, StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault();
						if (session == null)
						{
							user = dataContext.Users.Include("Country").Include("File").Where(u => u.Email.Equals(email, StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault();
							session = new Session()
							{
								ConnectedOn = DateTime.Now,
								LastAccess = DateTime.Now,
								User = user
							};
							dataContext.Sessions.Add(session);
						}
						else
						{
							session.ConnectedOn = DateTime.Now;
							session.LastAccess = DateTime.Now;
						}
						dataContext.SaveChanges();
					}
					Session["session"] = session;
				}
			}

		}

		protected override void BindLabels()
		{
			//throw new NotImplementedException();
		}

		protected override void BindViewModel()
		{
			//throw new NotImplementedException();
		}

		protected override void CreateViewModel()
		{
			//throw new NotImplementedException();
		}
	}
}